import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_study/widgets/public/app_form_group/widgets/app_form_input.dart';
import 'package:flutter_study/widgets/public/app_form_group/widgets/app_form_label.dart';
import 'package:flutter_study/widgets/public/themes/index.dart';
import 'package:rc_widget/rc_widget.dart';

class AppFormGroup extends StatelessWidget {
  const AppFormGroup(
      {super.key,
      this.labelText,
      this.labelLeft,
      this.labelRight,
      this.label,
      this.type = AppFormInputType.input,
      this.inputGroup,
      this.prefix,
      this.prepend,
      this.suffix,
      this.append,
      this.input,
      this.onTap,
      this.height,
      this.controller,
      this.hintText,
      this.inputFormatters,
      this.text,
      this.message,
      this.bottom});

  final String? labelText;
  final Widget? labelLeft;
  final Widget? labelRight;
  final Widget? label;

  final AppFormInputType type;

  final Widget? inputGroup;
  final String? prefix;
  final Widget? prepend;
  final String? suffix;
  final Widget? append;
  final Widget? input;
  final VoidCallback? onTap;
  final double? height;

  final TextEditingController? controller;
  final String? hintText;
  final List<TextInputFormatter>? inputFormatters;
  final String? text;

  final String? message;
  final Widget? bottom;

  factory AppFormGroup.input({
    required String labelText,
    TextEditingController? controller,
    required String hintText,
    List<TextInputFormatter>? inputFormatters,
    String? suffix,
    Widget? append,
    String? message,
    Widget? bottom,
  }) {
    return AppFormGroup(
      type: AppFormInputType.input,
      labelText: labelText,
      controller: controller,
      hintText: hintText,
      inputFormatters: inputFormatters,
      suffix: suffix,
      append: append,
      message: message,
      bottom: bottom,
    );
  }

  factory AppFormGroup.password({
    required String labelText,
    TextEditingController? controller,
    String? hintText,
    String? message,
    Widget? bottom,
  }) {
    return AppFormGroup(
      type: AppFormInputType.password,
      labelText: labelText,
      controller: controller,
      hintText: hintText,
      message: message,
      bottom: bottom,
    );
  }

  factory AppFormGroup.select({
    required String labelText,
    String? text,
    String? hintText,
    VoidCallback? onSelect,
    String? message,
    Widget? bottom,
  }) {
    return AppFormGroup(
      type: AppFormInputType.select,
      labelText: labelText,
      text: text,
      hintText: hintText,
      onTap: onSelect,
      message: message,
      bottom: bottom,
    );
  }

  factory AppFormGroup.text({
    required String labelText,
    required String text,
    String? message,
    Widget? bottom,
  }) {
    return AppFormGroup(
      type: AppFormInputType.text,
      labelText: labelText,
      text: text,
      message: message,
      bottom: bottom,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 40.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildLabel(),
          AppGap.h16,
          _buildFormInput(),
          _buildMessage()
        ],
      ),
    );
  }
  
  _buildLabel() {
     return label ??
        AppFormLabel(
          label: labelText,
          left: labelLeft,
          right: labelRight,
        );
  }
  
 Widget  _buildFormInput() {
    return inputGroup ?? AppFormInput(
      type: type,
      controller: controller,
      hintText: hintText,
      inputFormatters: inputFormatters,
      text:text, 
      suffix: suffix,
      append: append,
      onTap: onTap,
    );
  }
  
Widget _buildMessage() {
  if(message != null){
    return Container(
      margin: EdgeInsets.only(top: 8.h),
      child: Text('$message',style: AppThemes.of().w400Text228,),
    );
  }

  if(bottom != null){
    return Container(
      margin: EdgeInsets.only(top: 8.h),
      child: bottom,
    );
  }

  return const SizedBox.shrink();
}
}
